home *** CD-ROM | disk | FTP | other *** search
- Path: altair.dur.ac.uk!d40v94
- Newsgroups: comp.lang.c
- Subject: Help: array of ptrs to structures
- Message-ID: <Pine.SOL.3.91-941213.960403155531.22205C-100000@altair.dur.ac.uk>
- From: Ed Wiles <E.D.Wiles@durham.ac.uk>
- Date: Wed, 3 Apr 1996 16:18:31 +0100
- Organization: University of Durham, Durham, UK.
- NNTP-Posting-Host: altair.dur.ac.uk
- Mime-Version: 1.0
- Content-Type: TEXT/PLAIN; charset=US-ASCII
-
- [I'm a bit of a C novice, would appreciate your (combined) help, thanks!]
-
- "struct person" contains three variables: lastname, firstname, phone
- (all arrays of char).
-
- Within main(), I've declared an array of pointers to "struct person":
- struct person *phone_book[6];
-
- 1) How do I hard-code, for example, the third person in the array to be:
- lastname = "Adcock"; firstname = "Lucy"; phone = "(01623) 943086"; ?
-
- *phone_book[2] = {"Adcock", "Lucy", etc.} is not allowed, so do I have to
- assign each structure member individually?
- i.e. *phone_book[2]->lastname = "Adcock", etc.
-
- (In fact, I really want to hard-code all the people in the array in one
- go: is there a way of doing this when I declare the variable phone_book,
- remembering that e.g. int odds[] = {1, 3, 5, 7} is allowed?)
-
- Also, the pointer phone_book[2] is uninitialised, so it could be the
- address of anywhere... How do I initialise a pointer, please? (It is
- NEW(pointer_name) in Modula-2, so there must be a C equivalent..?)
-
- 2) I want to pass the entire array to a function. Do I just pass it as
- phone_book? Should the function's corresponding formal parameter be
- struct person *anyname[] ?
-
- Thanks a lot!
-
- - Ed Wiles (e.d.wiles@dur.ac.uk)
-